package com.slunk.platforms;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.Animation;
import com.badlogic.gdx.graphics.g2d.Sprite;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g2d.TextureAtlas.AtlasRegion;
import com.badlogic.gdx.graphics.g2d.TextureRegion;
import com.badlogic.gdx.utils.Array;

import com.slunk.interfaces.STargetedObjectInterface;

public class SRenderSprite extends SRenderableNode implements STargetedObjectInterface {
	private Sprite sprite;
	private Animation animation; 
	private boolean animating = false;
	private boolean looping = true;
	private float stateTime;        
	
	
	public SRenderSprite() {
		//lets test this out actually
		this.sprite = new Sprite();
	}
	
	/**
	 * Main constructor
	 * @param sprite The Sprite to be rendered by this class
	 */
	public SRenderSprite(Sprite sprite) {
		this.sprite = sprite;
	}
	/**
	 * Helper constructor that funnels new Sprite instance to the main constructor using a Texture instance
	 * @param texture The Texture instance to funnel  to main constructor
	 */
	public SRenderSprite(Texture texture) {
		this(new Sprite(texture));
	}
	/**
	 * Helper constructor that funnels new Texture  instance to the main constructor using a file path
	 * @param file
	 */
	public SRenderSprite(String file) {
		this(new Texture(Gdx.files.internal(file)));
	}
	/**
	 * Helper constructor that funnels a new Sprite instance to the main constructor
	 * @param texture
	 * @param srcX
	 * @param srcY
	 * @param srcWidth
	 * @param srcHeight
	 */
	public SRenderSprite(Texture texture, int srcX, int srcY, int srcWidth, int srcHeight) {
		this(new Sprite(texture,srcX,srcY,srcWidth,srcHeight));
	}
	/**
	 * Helper constructor that funnels a new Sprite instance to the main constructor using a texture region
	 * @param region
	 */
	public SRenderSprite(TextureRegion region) {
		this(new Sprite(region));
	}
	/**
	 * Helper constructor that funnels a new Sprite instance to the main constructor using a texture region
	 * @param region
	 * @param x
	 * @param y
	 * @param width
	 * @param height
	 */
	public SRenderSprite(TextureRegion region, int x, int y, int width, int height) {
		this(new Sprite(region, x, y, width, height));
	}
	/**
	 * Returns the sprite
	 * @return
	 */
	public Sprite getSprite() {
		return sprite;
	}
	
	public void setSpriteRegion(AtlasRegion aRegion) {
		this.sprite.setRegion(aRegion);
		this.sprite.setBounds(this.sprite.getX(), this.sprite.getY(), aRegion.getRegionWidth(), aRegion.getRegionHeight());
		animating = false;
	}
	
	/**
	 * Sets the visibility of the sprite.
	 * @param isVisible
	 */
	public void setVisible(boolean isVisible) {
		if (isVisible && getAlpha() != 1.0f) {
			setAlpha(1.0f);
		} else if (!isVisible && getAlpha() != 0.0f) {
			setAlpha(0.0f);
		}
	}
	public void setAlpha(float a) {
		Color oldColor = sprite.getColor();
		sprite.setColor(oldColor.r, oldColor.g, oldColor.b, a);
	}
	public float getAlpha() {
		return sprite.getColor().a;
	}
	public void setPosition(float x, float y) {
		sprite.setPosition(x,y);
	}
	public float getWidth() {
		return sprite.getWidth();
	}
	public float getHeight() {
		return sprite.getHeight();
	}
	
	public void setAnimation(Array<AtlasRegion> frames, float frameDuration, boolean looping, int playMode) {
		animation = new Animation(frameDuration, frames, playMode);
		stateTime = 0.0f;
		animating = true;
		this.looping = looping; 
		
		System.out.println("setting bounds again");
		//just set the initial bounding box
		AtlasRegion aRegion = frames.first();
		this.sprite.setBounds(this.sprite.getX(), this.sprite.getY(), aRegion.getRegionWidth(), aRegion.getRegionHeight());
	}
	
	public void drawObject(SpriteBatch batch) {
		if (animating && !animation.isAnimationFinished(stateTime)) {
			//if is animating, get the next frame and set the sprite to it
			stateTime += Gdx.graphics.getDeltaTime();   
			TextureRegion region = animation.getKeyFrame(stateTime, looping);
			sprite.setRegion(animation.getKeyFrame(stateTime, looping),0,0,region.getRegionWidth(),region.getRegionHeight());  
			sprite.setSize(region.getRegionWidth(), region.getRegionHeight());
		} else {
			animating = false;
			stateTime = 0.0f;
			looping = false;
			animation = null;
		}
		sprite.draw(batch);
	}
	
	public void onEnter() {
		//do nothing right now
	}
	public void onExit() {
		//do nothing right now
	}
	@Override
	public Boolean containsTouch(float x, float y) {
		return sprite.getBoundingRectangle().contains(x, y);
	}
	@Override
	public Boolean touchDown(float x, float y) {
		return isTouchEnabled;
	}

	@Override
	public Boolean touchDragged() {
		return isTouchEnabled;
	}
	@Override
	public Boolean touchUp() {
		return isTouchEnabled;
	}
	@Override
	public Boolean mouseMoved(float x, float y) {
		return true;
	}
	@Override
	public int compareTo(SRenderableNode o) {
		// TODO Auto-generated method stub
		//return 0;
		return z_order - o.z_order;
	}
}
